using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace minega
{
	public partial class frmGridSize : Form
	{
		private Grid _grid;

		public frmGridSize()
		{
			InitializeComponent();
			numWidth.Maximum = numHeight.Maximum = UInt16.MaxValue;
			numMines.Maximum = numWidth.Maximum * numHeight.Maximum - 1;
		}

		public frmGridSize(ref Grid grid)
		{
			InitializeComponent();
			_grid = grid;
			numWidth.Maximum = numHeight.Maximum = numMines.Maximum = decimal.MaxValue;
			numWidth.Value = _grid.Width;
			numHeight.Value = _grid.Height;
			numMines.Value = _grid.Mines;
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			decimal mines = numMines.Value;
			if (numMines.DecimalPlaces > 0)
				mines = numMines.Value * numWidth.Value * numHeight.Value;
			_grid.NewGame((int)numWidth.Value, (int)numHeight.Value, (int)mines);
			Close();
		}

		private void radioButton_CheckedChanged(object sender, EventArgs e)
		{
			if (((RadioButton)sender).Checked)
			{
				if (sender == rdoAmount)
				{
					decimal val = numMines.Value;
					if (val <= 1)
					{
						val *= numWidth.Value * numHeight.Value;
					}
					numMines.Maximum = numWidth.Value * numHeight.Value;
					numMines.Increment = 1;
					numMines.DecimalPlaces = 0;
					numMines.Value = (int)val;
				}
				else if (sender == rdoRatio)
				{
					decimal val = numMines.Value;
					if (val > 1)
					{
						val /= numWidth.Value * numHeight.Value;
					}
					numMines.Maximum = 1.0M;
					numMines.Increment = 0.005M;
					numMines.DecimalPlaces = 3;
					numMines.Value = val;
				}
			}
		}
	}
}